package com.modular.iot.action;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.*;
import cn.hutool.db.Entity;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import com.ToolOpt;
import com.modular.iot.bean.ThingsAttributeDo;
import com.modular.iot.tool.ToolIotCmd;
import ntd.action.ActionAbs;
import ntd.action.bo.ActionInfo;
import ntd.action.bo.ActionOutBo;
import ntd.util.config.common.exception.ComException;
import ntd.util.frame.action.iot.bo.SmartEmissionAgreementBo;
import ntd.util.frame.action.iot.cache.SmartEmissionAgreementCache;
import ntd.util.frame.action.iot.context.IotContext;
import ntd.util.frame.data.factory.FactoryData;
import ntd.util.frame.data.tool.ToolMysql;
import ntd.util.modular.data.util.handle.tool.Tool212;

import java.sql.SQLException;
import java.util.List;

public class ActionIotDeviceCmdExe extends ActionAbs {

    @Override
    public ActionInfo info() {
        return new ActionInfo("设备指令执行", "iot-device-cmd-exe", new ActionIotDeviceCmdExe());
    }

    
    @Override
    public ActionOutBo handle() {

        // 操作功能编码
        String serverCode = actionInBo.getParamStr("serverCode");

        IotContext iotContext = IotContext.init(actionInBo.getDataCode());

        // 阀门控制
        if ("fmkz".equals(serverCode)) {
            return new ActionOutBo(handleFmkz(iotContext));
        }

        return new ActionOutBo("true");
    }

    /**
     * 阀门控制执行
     * @param iotContext
     * @return
     */
    private SmartEmissionAgreementBo handleFmkz(IotContext iotContext) {
        // 阀门动作 - 1 开，2 关
        String fmAction = actionInBo.getParamStr("fm-action");
        // 阀门编号 - 0:排污阀 1:应急阀
        String fmNum = actionInBo.getParamStr("fm-num");

        SmartEmissionAgreementBo smartEmissionAgreementBo = SmartEmissionAgreementCache.get(iotContext.getDeviceCode());

        // 更新设备运行状态
        String autoRunStr = ObjectUtil.defaultIfBlank(actionInBo.getParamStr("auto_run"), actionInBo.getParamStr("auto-run"));
        if (StrUtil.isNotBlank(autoRunStr) && BooleanUtil.toBoolean(autoRunStr) != smartEmissionAgreementBo.isAutoRun()) {
            smartEmissionAgreementBo.setAutoRun(BooleanUtil.toBoolean(autoRunStr));
            SmartEmissionAgreementCache.update(iotContext.getDeviceCode(), "auto_manager", smartEmissionAgreementBo.isAutoRun());
        }

        if (StrUtil.isNotBlank(fmNum) && StrUtil.isNotBlank(fmAction)) {
            SmartEmissionAgreementBo smartEmissionAgreementBoClone = new SmartEmissionAgreementBo();

            BeanUtil.copyProperties(smartEmissionAgreementBo, smartEmissionAgreementBoClone);

            if (smartEmissionAgreementBoClone.isAutoRun()) {
                throw new ComException("请先调整设备为手动控制再进行对应操作...");
            }

            if ("0".equals(fmNum)) {
                smartEmissionAgreementBoClone.setValve("1".equals(fmAction));
            }
            if ("1".equals(fmNum)) {
                smartEmissionAgreementBoClone.setValve2("1".equals(fmAction));
            }

            // 执行规则
            ToolIotCmd.pushCmd(iotContext.getDeviceCode(), Tool212.build212Bo(iotContext.getDeviceCode(), smartEmissionAgreementBoClone).to212Str(),
                    ToolIotCmd.TYPE_212, "手动控制");
        }

        return smartEmissionAgreementBo;
    }

    /**
     * 更新设备配置信息
     * @param iotContext
     * @param configJson
     * @param autoRunStr
     */
    private static void updateDevice(IotContext iotContext, JSONObject configJson, String autoRunStr) {
        Entity deviceConfig = iotContext.getDeviceConfig();
        configJson.set("auto-run", autoRunStr);
        deviceConfig.set("config", configJson.toString());
        ToolMysql toolMysql = new ToolMysql(FactoryData.dataSource());
        try {
            toolMysql.db.insertOrUpdate(deviceConfig, "id");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
